<!DOCTYPE html>
<html dir="ltr">

<head>
  <meta charset="UTF-8">
  <title>Nav Pop</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link href="../../../../../css/ionic.bundle.css" rel="stylesheet">
  <link href="../../../../../scripts/testing/styles.css" rel="stylesheet">
  <script src="../../../../../scripts/testing/scripts.js"></script>
  <script src="../../../../../dist/ionic.js"></script>
</head>

<body onload="loadFirstPage()">
  <ion-app>
    <ion-nav></ion-nav>
  </ion-app>
  <style>
    f {
      display: block;
      margin: 15px auto;
      max-width: 150px;
      height: 150px;
      background: blue;
    }

    f:last-of-type {
      background: yellow;
    }

  </style>
</body>

<script>

  async function loadFirstPage() {
    const nav = document.querySelector('ion-nav');
    await nav.componentOnReady();
    const firstPage = document.createElement('div');
    firstPage.classList.add('first-page');
    firstPage.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-title>Page One</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content padding>
        <h1>Page One</h1>
        <ion-button class="next">Go to Page Two</ion-button>
      </ion-content>
    `;

    await nav.push(firstPage);

    // okay cool, we're in the DOM now
    const button = firstPage.querySelector('ion-button');
    button.addEventListener('click', async () => {
      await goToPageTwo(nav);
    });
  }

  async function goToPageTwo(nav) {
    const secondPage = document.createElement('div');
    secondPage.classList.add('second-page');
    secondPage.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-title>Page Two</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content padding>
        <h1>Page Two</h1>
        <ion-button class="next">Go to Page Three</ion-button>
        <ion-nav-pop>
          <ion-button class="previous">Go Back</ion-button>
        </ion-nav-pop>
      </ion-content>
    `;

    // okay cool, we're in the DOM now
    await nav.push(secondPage);

    const nextButton = secondPage.querySelector('ion-button.next');
    nextButton.addEventListener('click', async () => {
      await goToPageThree(nav);
    });
  }

  async function goToPageThree(nav) {
    const thirdPage = document.createElement('div');
    thirdPage.classList.add('third-page');
    thirdPage.innerHTML = `
      <ion-header>
        <ion-toolbar>
          <ion-title>Page Three</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content padding>
        <h1>Page Three</h1>
        <ion-nav-pop>
          <ion-button class="previous">Go Back</ion-button>
        </ion-nav-pop>
      </ion-content>
    `;

    // okay cool, we're in the DOM now
    await nav.push(thirdPage);
  }
</script>

</html>
